//
//  HIScrollMenu.swift
//  Health_Infomation
//
//  Created by 林帆 on 2017/8/22.
//  Copyright © 2017年 林帆. All rights reserved.
//

import UIKit

@objc protocol HIScrollMenuDelegate{
    func HIScrollMenuDelegateDo()
}

let btnWidth = 80

class HIScrollMenu: UIView {
    
    weak var HIScrollDelegate : HIScrollMenuDelegate? = nil
    lazy var MenuEditBtn = UIButton()
    lazy var MenuScrollVIew = UIScrollView()
    var DataArray = ["",""]
    let MenuBtnWidth : CGFloat = 42
    
    convenience init(itemArray: [String] , frame: CGRect) {
        self.init(frame: frame)
        DataArray = itemArray
        ConfigScrollVIew()
        ConfigEditBtn()
    }
    
    func  ConfigScrollVIew() {
        
        MenuScrollVIew.backgroundColor = UIColor.red
        MenuScrollVIew.showsHorizontalScrollIndicator = false
        MenuScrollVIew.showsVerticalScrollIndicator = false
        MenuScrollVIew.bounces = false
        MenuScrollVIew.isPagingEnabled = false
        MenuScrollVIew.contentSize = CGSize(width: 80.0 * CGFloat(DataArray.count), height: self.frame.size.height)
        MenuScrollVIew.delegate = self
        MenuScrollVIew.frame = CGRect(x: 0, y: 0, width: self.frame.size.width - MenuBtnWidth, height: self.frame.size.height)
        self.addSubview(MenuScrollVIew)
        
        //加itemBtn
        
        for i in 0..<DataArray.count {
            let Btn = UIButton()
            Btn.setTitle(DataArray[i], for: .normal)
            Btn.tag = i + 123
            Btn.addTarget(self, action: #selector(itemClick(tag:)), for: .touchUpInside)
            Btn.frame = CGRect(x: btnWidth * i, y: 0, width: btnWidth, height: 44)
            Btn.layer.cornerRadius = 5
            Btn.layer.borderColor = UIColor.red.cgColor
            Btn.layer.borderWidth = 0.5
            MenuScrollVIew.addSubview(Btn)
        }
    }
    
    func  ConfigEditBtn() {
        MenuEditBtn.frame = CGRect(x: self.frame.size.width - MenuBtnWidth, y: 0, width: MenuBtnWidth, height: self.frame.size.height)
        MenuEditBtn.addTarget(self, action: #selector(MenuEditBtnClick), for: .touchUpInside)
        MenuEditBtn.setImage(UIImage(named: "加号1"), for: .normal)
        MenuEditBtn.backgroundColor = UIColor.gray
        self.addSubview(MenuEditBtn)
    }
    
    func itemClick(tag : NSString) {
//        for i in 0..<DataArray.count {
//           let Btn = self.viewWithTag(i + 123)
//           Btn?.removeFromSuperview()
//        }
    }
    
    func ReConfigItemBtn(dataArray: Array<String>) {
        
        if DataArray == dataArray {  //如果没有改变的话 返回
           return
        }
        
        for i in 0..<DataArray.count {
            let Btn = self.viewWithTag(i + 123)
            Btn?.removeFromSuperview()
        }
        DataArray = dataArray
        MenuScrollVIew.contentSize = CGSize(width: 80.0 * CGFloat(DataArray.count), height: self.frame.size.height)
        for i in 0..<DataArray.count {
            let Btn = UIButton()
            Btn.setTitle(DataArray[i] , for: .normal)
            Btn.tag = i + 123
            Btn.addTarget(self, action: #selector(itemClick(tag:)), for: .touchUpInside)
            Btn.frame = CGRect(x: btnWidth * i, y: 0, width: btnWidth, height: 44)
            Btn.layer.cornerRadius = 5
            Btn.layer.borderColor = UIColor.red.cgColor
            Btn.layer.borderWidth = 0.5
            MenuScrollVIew.addSubview(Btn)
        }
    }
    
    func MenuEditBtnClick () {
        self.HIScrollDelegate?.HIScrollMenuDelegateDo()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension HIScrollMenu: UIScrollViewDelegate {
    
   
    
    
}







